package nlp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.bea.jvm.MethodProfileEntry;
import com.bea.jvm.ProfilingSystem;

import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
//JVM profiling imports
import com.bea.jvm.NotAvailableException;
import com.bea.jvm.UnapplicableMethodException;
import java.lang.reflect.Method;
/**
 * @author Sebastian Suarez Benjumea ssuarezbe@unal.edu.co
 *
 */
public class ParallelPartOfSpeech extends Thread {
	private String pathmodelFile;
	private POSTaggerME tagger;
	private NlpTokenizer tokenizer;
	//Attributes used for the Profiling
	private ProfilingSystem profiler;
	public MethodProfileEntry methodProfileEntry;
	public Method method;
	Class params[]={String.class};
	//Attributes used in the method "tagSentence()"in order to avoid create them all the time
	String taggedSentece;
	String tokens[];
	String tags[];
	double tagProb[];
	int i;
	//Attributes used for parallel processing
	String [] sentences;
	public String [] taggedSentences;
	int currSent;
	/**
	 * @param File The configuration file that contain the model for the Sentence Segmentation process
	 */
	public ParallelPartOfSpeech(String pathConfigFile,String path,ProfilingSystem profiler){
		this.pathmodelFile=pathConfigFile;
		// get Profiling System 
		this.profiler = profiler;
		this.tokenizer=new NlpTokenizer(path+"en-token.bin",profiler);
		this.tokenizer.init();
			}

	/**
	 * This method load the model in order to instantiate a SentenceDetectorME.
	 * It uses the File loaded in the constructor (attribute="modelFile") as the model file.
	 * Is should only call once in order to create only one "SentenceExtractorME". 
	 */
	public void init(){
		try{
		InputStream modelIn=new FileInputStream(this.pathmodelFile);
		POSModel model = new POSModel(modelIn);
		this.tagger = new POSTaggerME(model);
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		/**
		 * Definition of the JRockit profiling code lines
		 * For more info check:
		 * http://www.rgagnon.com/javadetails/java-0031.html
		 * http://www.tomecode.com/2010/10/02/profiling-java-ap-with-jmapi-jrockit/
		 */
		// get method append from StringBuffer class
		try {
			this.method = PartOfSpeechTag.class.getDeclaredMethod("tagSentence", params);
			// set method to profiler
			this.methodProfileEntry = profiler.newMethodProfileEntry(method);
			this.methodProfileEntry.setInvocationCountEnabled(true);
			this.methodProfileEntry.setTimingEnabled(true);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotAvailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnapplicableMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * This method receives a line tags the sentence returning the structure "token|tag|probability confidence score of the tag"
	 *@param String  Line to be split.
	 *@return  String[] sentences array.
	 */
	public String tagSentence(String sentence){
		this.taggedSentece="";
		this.tokens=tokenizer.tokenizeLine(sentence);
		this.tags=tagger.tag(tokens);
		this.tagProb=tagger.probs();
		for(this.i=0;this.i<this.tokens.length;this.i++){
			if(this.i==this.tokens.length-1){
				/*The last column should not have "/" in order to makes the processing of the
				 * information easier. 
				 */
				this.taggedSentece+=this.tokens[this.i]+"|"+this.tags[this.i]+"|"+this.tagProb[this.i];
			}else{
				this.taggedSentece+=this.tokens[this.i]+"|"+tags[this.i]+"|"+tagProb[this.i]+"/";
			}
		}
		return this.taggedSentece;
	}
	
	public void initSentArray(String [] sentArray){
		this.sentences=new String[sentArray.length];
		this.taggedSentences=new String[sentArray.length];
		this.sentences=sentArray;
	}
	
	public void performTasks(){
		for(this.currSent=0;this.currSent<this.sentences.length;this.currSent++){
			this.taggedSentences[this.currSent]=tagSentence(this.sentences[this.currSent]);
		}
	}

	@Override
	public void run() {
		
	}
}
